package com.example.app2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.app2.R;
import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager viewPager = findViewById(R.id.viewPager);
        //创建一个适配器，并获取一个fragmen管理者
        MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        //获取标签视图
        TabLayout tabLayout = findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.getTabAt(0).setIcon(R.drawable.xx2);
        tabLayout.getTabAt(1).setIcon(R.drawable.kk);
        tabLayout.getTabAt(2).setIcon(R.drawable.xx);
        tabLayout.getTabAt(3).setIcon(R.drawable.ff);


        // 设置点击事件
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition(), false); // 快速跳转页面
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

    }

    public class MyPagerAdapter extends FragmentPagerAdapter {
        private static final int PAGE_COUNT = 4;

        public MyPagerAdapter(FragmentManager fm) {
            super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        }

        //每一个标签对应一个页面
        @NonNull
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new MessagesFragment();
                case 1:
                    return new ContactsFragment();
                case 2:
                    return new DiscoverFragment();
                case 3:
                    return new zdyFragment();
                default:
                    return null;
            }
        }

        //标签的个数
        @Override
        public int getCount() {
            return PAGE_COUNT;
        }
        //显示标签的内容
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "消息";
                case 1:
                    return "通讯录";
                case 2:
                    return "朋友圈";
                case 3:
                    return "我";
                default:
                    return null;
            }
        }
    }

    public static class MessagesFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_messages, container, false);
        }
    }

    public static class ContactsFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_contacts, container, false);
        }
    }

    public static class DiscoverFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_discover, container, false);
        }
    }

    public static class zdyFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_zdy, container, false);
        }
    }
}
